import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {BaseComponent} from '../common/base.component';
import {UserService} from './user.service';
import {Md5} from 'ts-md5/dist/md5';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent extends BaseComponent implements OnInit {
  password: string;
  account: string;

  constructor(private userService: UserService, private router: Router) {
    super();
  }

  ngOnInit() {
  }

  onLogin() {
    const pwdMd5 = Md5.hashStr(this.password).toString();
    this.userService.login(this.account, pwdMd5)
      .then(res => {
        if (res[0]) {
          this.router.navigate(['/business']);
        } else {
          this.alert.error(res[1]);
        }
      })
      .catch(error => this.alert.error('登录失败'));
  }
}
